package com.example.demo;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/3/8 10:47
 */
public class ConfigDemo2Servlet extends HttpServlet {
    String servletName = null;
    ServletConfig config = null;
    String email = null;
    String postcode = null;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.config = config;
        servletName = config.getServletName();
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        email = (String) servletContext.getInitParameter("email");
        postcode = (String) servletContext.getInitParameter("postcode");
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("Servlet名称：" + servletName + "<br>");
        out.println("Email地址：" + email + "<br>");
        out.println("邮编：" + postcode);
        out.println("</body></html>");
    }
}
